// pages/tv/tv.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        videoUrl:'',
        danmu:{
            text:'',
            color:'pink'
        },
        imgUrl:'',
        audio_poster:'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
        audio_src:'../../assets/audio/赵传 - 爱要怎么说出口.mp3',
        audio_name:'此时此刻',
        audio_author:'许巍',
        lusrc:'',
        zp:'',
        lx:'',
        lxtp:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this;
        //获取全局唯一的录音管理器 RecorderManager实例
        that.recorderManager = wx.getRecorderManager()
        that.recorderManager.onStop((res) => {
          that.setData({
            lusrc: res.tempFilePath // 文件临时路径
          })
          console.log('获取到文件：' + that.data.lusrc)
        })
        this.recorderManager.onError((res) => {
          console.log('录音失败了！')
          //console.log(res)
        })
    },
    chooseVideo(){
        let that = this
        wx.chooseVideo({
            sourceType:['album'],
            success:(res)=>{
                that.setData({
                    videoUrl:res.tempFilePath
                })
                console.log(res.tempFilePath);
            }
        })
    },
    choosePic(){
        let that = this
        wx.chooseImage({
          count: 1,
          success:res=>{
              that.setData({
                imgUrl:res.tempFilePaths[0]
              })
            console.log(res.tempFilePaths[0]);
          }
        })
    },
    savePic(){
        let that = this
        wx.saveImageToPhotosAlbum({
          filePath: that.data.imgUrl,
          success:res=>{
              console.log(res);
          }
        })
    },
    preview(){
        let that = this
        wx.previewImage({
          urls: [that.data.imgUrl],
          success:res=>{
              console.log(res);
          }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        this.VideoContext = wx.createVideoContext('video1')
        this.audioCtx = wx.createAudioContext('myAudio')
        this.ctx = wx.createCameraContext()
    },
    // 相机
    takePhoto(){
        let that = this
        this.ctx.takePhoto({
            quality:'high',
            success: (res) => {
                that.setData({
                  zp: res.tempImagePath
                })
            }
        })
    },
    startRecord(){
        this.ctx.startRecord({
            success: (res) => {
              console.log('startRecord')
            }
        })
    },
    stopRecord() {
        this.ctx.stopRecord({
          success: (res) => {
            console.log('chengg');
            this.setData({
              lxtp: res.tempThumbPath,
              lx: res.tempVideoPath
            })
          },
          fail:res=>{
            console.log(res);
          }
        })
      },
    // 录音
    ly(){
        this.recorderManager.start({
            duration: 60000,
            sampleRate: 16000, //采样率，有效值 8000/16000/44100
            numberOfChannels: 1, //录音通道数，有效值 1/2
            encodeBitRate: 96000, //编码码率
            format: 'mp3', //音频格式，有效值 aac/mp3
            frameSize: 50, //指定帧大小
            audioSource: 'auto' //指定录音的音频输入源，可通过 wx.getAvailableAudioSources() 获取
          })
    },
    jsly(){
        this.recorderManager.stop()
    },
    bfly(){
        // 获取innerAudioContext实例
        const innerAudioContext = wx.createInnerAudioContext()
        // 是否自动播放
        innerAudioContext.autoplay = true
        // 设置音频文件的路径
        innerAudioContext.src = this.data.lusrc;
        // 播放音频文件
        wx.playVoice({
          filePath: this.data.lusrc,
          success:()=>{
              console.log('正在播放');
          },
          fail:()=>{
              console.log('播放失败');
          }
        })
    },
    // 音频
    audioPlay(){
        this.audioCtx.play()
    },
    audioPause(){
        this.audioCtx.pause()
    },
    audio14(){
        this.audioCtx.seek(14)
    },
    audioStart(){
        this.audioCtx.seek(0)
    },
    // 视频
    play(){
        this.VideoContext.play()
    },
    pause(){
        this.VideoContext.pause()
    },
    replay(){
        this.VideoContext.seek(0)
        this.VideoContext.play()
    },
    getName(event){
        console.log(event.detail.value);
        this.setData({
            danmu:{
                text:event.detail.value,
                color:'pink'
            }
        })
    },
    sendDanmu(){
        let danmu = this.data.danmu
        console.log(danmu);
        this.VideoContext.sendDanmu(danmu)
        let color = this.data.danmu.color
        this.setData({
            danmu:{
                text:'',
                color:color
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})